using System;
using System.Collections.Generic;
using System.Text;
using Dominio;
using NHibernate;
using NHibernate.Cfg;

namespace DAP
{
    public class CompanyRepository : AbstractRepository
    {
        public Company getCompanyById(int id)
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            try
            {
                IQuery query = sesion.CreateQuery("from Company where IdCompany = :x");
                query.SetString("x", id.ToString());
                transaction.Commit();
                return (Company)query.UniqueResult();
            }
            catch
            {
                transaction.Rollback();
                throw new HibernateException();
            }
        }

        public void saveOrUpdateCompany (Company company)
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            try
            {
                sesion.SaveOrUpdate(company);
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw new HibernateException();
            }
        }
    }
}
